<template>
  <div>
    <!--  v-model="currentDate" 双向绑定时间选择器 -->
    <!--  -->
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onFirm"
    />
  </div>
</template>

<script>
import { updateUserName } from "@/api/user.js";
import dayjs from "dayjs";
export default {
  name: "UpdateBirthday",
  data() {
    return {
      minDate: new Date(1980, 1, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    };
  },
  props: {
    value: {
      type: String,
      required: true,
    },
  },
  methods: {
    async onFirm() {
      this.$toast.loading({
        message: "修改中",
        forbidClick: true, //背景禁止点击
        duration: 0, //持续展示
      });
      try {
        // dayjs  日期格式化  format
        const currentDate = dayjs(this.currentDate).format("YYYY-MM-DD");
        const { data } = await updateUserName({
          birthday: currentDate,
          // 修改谁就写谁
        });
        console.log(data.data);
        // v-modle触发input事件
        // 更新视图
        this.$emit("input", currentDate);
        // 关闭弹层
        this.$emit("close");
        // 提示成功;
        this.$toast.success("更新成功");
      } catch (err) {
        this.$toast.fail("日期更新失败");
      }
    },
  },
};
</script>

<style>
</style>